package com.naiterui.ehp.bs.emr.dao.mongo;

import com.naiterui.ehp.bs.emr.entity.mongo.MedicalRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Deprecated
@Repository
public class DrRecordDao extends MongoBaseDao {

    private static final String COLLECTION_NAME = "medical_record";

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    protected MongoTemplate getMongoTemplate() {
        return mongoTemplate;
    }

    /**
     * 调用患者最近发送本人病历
     * DrRecordDao.getPatientSendLateCase()
     *
     * @param doctorId
     * @param patientId
     *
     * @return
     *
     * @Author lichaopi
     * @Date 2018年5月29日
     * @since 1.0.0
     */
    public MedicalRecord getPatientSendLateCase(Long doctorId, Long patientId) {
        Criteria c3 = new Criteria().orOperator(Criteria.where("caseType").is(MedicalRecord.CASETYPE_PATIENTRECORD),
                                                Criteria.where("caseType").is(MedicalRecord.CASETYPE_CONSULT));
        Criteria criteria = Criteria.where("doctorId").is(doctorId).and("patientId").is(patientId).and("relation")
                                    .is(MedicalRecord.ERelation.RELATION_ONESELF.getValue());
        criteria.andOperator(c3);

        Query query = Query.query(criteria).with(Sort.by(new Sort.Order(Direction.DESC, "createAt"))).limit(1);
        List<MedicalRecord> list = super.find(query, MedicalRecord.class, COLLECTION_NAME);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 病历列表
     * DrRecordDao.queryMedicalRecordList()
     *
     * @param doctorId
     * @param patientId
     * @param pageSize
     *
     * @return
     *
     * @Author lichaopi
     * @Date 2018年5月31日
     * @since 1.0.0
     */
    public List<MedicalRecord> queryMedicalRecordList(Long doctorId, Long patientId, Integer page, Integer pageSize, Integer relation, List<Long> inquirerIds) {
        Criteria c = getCriteriaCondition(doctorId, patientId, relation,inquirerIds);

        return super.queryByPage(COLLECTION_NAME, MedicalRecord.class, c, page, pageSize, "desc", "createAt");
    }

    /**
     * 病例数量
     * DrRecordDao.queryCount()
     *
     * @param doctorId
     * @param patientId
     *
     * @return
     *
     * @Author lichaopi
     * @Date 2018年5月31日
     * @since 1.0.0
     */
    public Long queryCount(Long doctorId, Long patientId, Integer relation, List<Long> inquirerIds) {
        Criteria c = getCriteriaCondition(doctorId, patientId, relation, inquirerIds);
        Query query = new Query();
        query.addCriteria(c);
        return super.queryCount(query, COLLECTION_NAME);
    }

    /**
     * 获取查询条件
     * DrRecordDao.getCriteriaCondition()
     *
     * @param doctorId
     * @param patientId
     * @param relation
     *
     * @return
     *
     * @Author lichaopi
     * @Date 2018年5月31日
     * @since 1.0.0
     */
    private Criteria getCriteriaCondition(Long doctorId, Long patientId, Integer relation, List<Long> inquirerIds) {
        Criteria c;
        // 全部关系
        if (relation == null) {
            Criteria c1 = Criteria.where("doctors.doctorId").is(doctorId).and("patientId").is(patientId)
                                  .and("temporary").ne(MedicalRecord.TEMPORARY_YES)
                                  .and("send").exists(MedicalRecord.SEND_YES)
                                  .and("available").ne(MedicalRecord.AVAILABLE_YES);
            c = new Criteria().orOperator(c1);
        } else if (relation.intValue() == MedicalRecord.ERelation.RELATION_ONESELF.getValue().intValue()) {
            Criteria c1 = Criteria.where("doctors.doctorId").is(doctorId)
                                  .and("patientId").is(patientId)
                                  .and("relation").is(relation)
                                  .and("send").exists(MedicalRecord.SEND_YES)
                                  .and("temporary").ne(MedicalRecord.TEMPORARY_YES)
                                  .and("available").ne(MedicalRecord.AVAILABLE_YES);
            c = new Criteria().orOperator(c1);
        } else {
            c = Criteria.where("doctors.doctorId").is(doctorId)
                        .and("patientId").is(patientId)
                        .and("relation").is(relation)
                        .and("send").exists(MedicalRecord.SEND_YES)
                        .and("temporary").ne(MedicalRecord.TEMPORARY_YES)
                        .and("available").ne(MedicalRecord.AVAILABLE_YES);

        }
        if(!CollectionUtils.isEmpty(inquirerIds)){
            c.and("inquirerId").in(inquirerIds);
        }
        return c;
    }


    /**
     * 查询患者最近病历（医生填写或患者发送）
     * DrRecordDao.getPtOrDrSendLateCase()
     *
     * @param doctorId
     * @param patientId
     *
     * @return
     *
     * @Author lichaopi
     * @Date 2018年5月31日
     * @since 1.0.0
     */
    public MedicalRecord getPtOrDrSendLateCase(Long doctorId, Long patientId, Integer relation) {
        Criteria c1 = Criteria.where("caseType").is(MedicalRecord.CASETYPE_PATIENTRECORD);
        Criteria c2 = Criteria.where("caseType").is(MedicalRecord.CASETYPE_DOCTORRECORD);
        Criteria c3 = Criteria.where("caseType").is(MedicalRecord.CASETYPE_DOCTORRECORD_RECOM).and("temporary").ne(MedicalRecord.TEMPORARY_YES);
        Criteria c0 = Criteria.where("caseType").is(MedicalRecord.CASETYPE_CONSULT);
        Criteria c4 = new Criteria().orOperator(c1, c2, c3, c0);
        Criteria c5;
        if (relation != null) {
            c5 = Criteria.where("doctorId").is(doctorId).and("patientId").is(patientId).and("relation").is(relation).andOperator(c4);
        } else {
            c5 = Criteria.where("doctorId").is(doctorId).and("patientId").is(patientId).andOperator(c4);
        }

        Query query = Query.query(c5).with(Sort.by(new Sort.Order(Direction.DESC, "createAt"))).limit(1);
        List<MedicalRecord> list = super.find(query, MedicalRecord.class, COLLECTION_NAME);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 获取指定时间内的患者医嘱提醒信息
     * DrRecordDao.getRevisitInfoList()
     *
     * @param doctorId
     * @param patientId
     * @param startTime
     * @param endTime
     *
     * @return
     *
     * @Date 2018年6月28日
     * @since 2.19
     */
    public List<MedicalRecord> getRevisitInfoList(Long doctorId, Long patientId, Date startTime, Date endTime) {
        Criteria criteria = Criteria.where("patientId").is(patientId).and("doctorId").is(doctorId);
        if (startTime != null && endTime == null) {
            criteria.and("recordDoctor.revisitDate").gte(startTime);
        } else if (startTime == null && endTime != null) {
            criteria.and("recordDoctor.revisitDate").lte(endTime);
        } else if (startTime != null) {
            criteria.andOperator(Criteria.where("recordDoctor.revisitDate").gte(startTime), Criteria.where("recordDoctor.revisitDate").lte(endTime));
        }
        Query query = new Query();
        query.addCriteria(criteria);
        return super.find(query, MedicalRecord.class, COLLECTION_NAME);
    }

    /**
     * 根据推荐id列表查询病历列表
     * DrRecordDao.getRecordListByRecomIdList()
     *
     * @param recomIdList
     *
     * @return
     *
     * @Author lichaopi
     * @Date 2018年6月30日
     * @since 1.0.0
     */
    public List<MedicalRecord> getRecordListByRecomIdList(List<Long> recomIdList) {
        List<MedicalRecord> mrList = null;
        if (recomIdList != null && !recomIdList.isEmpty()) {
            Criteria c = Criteria.where("recordDoctor.recom.recommendId").in(recomIdList);
            Query query = new Query();
            query.addCriteria(c);
            mrList = super.find(query, MedicalRecord.class, COLLECTION_NAME);
        }
        return mrList;
    }

}
